package com.vhall.component.service.account.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.account.OrgMapper;
import com.vhall.component.entity.account.OrgEntity;
import com.vhall.component.entity.account.vo.OrgListVO;
import com.vhall.component.service.account.BaseUserPermissionService;
import com.vhall.component.service.account.OrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * OrgServiceImpl-
 *
 * @author xing.wang
 * @date 2023/8/7
 */
@Slf4j
@Service
public class OrgServiceImpl extends BaseUserPermissionService implements OrgService {
    @Resource
    private OrgMapper orgMapper;

    @Override
    public List<OrgListVO> getRoleOrgList() {
        LambdaQueryWrapper<OrgEntity> queryWrapper = Wrappers.lambdaQuery();
        String userPermission = getUserPermission();
        queryWrapper.eq(StrUtil.isNotBlank(userPermission),OrgEntity::getOrgCode,userPermission);
        List<OrgEntity> orgEntities = orgMapper.selectList(queryWrapper);
        List<OrgListVO> result = new ArrayList<>();
        if (CollUtil.isNotEmpty(orgEntities)) {
            result = orgEntities.stream().map(data->{
                OrgListVO orgListVO = new OrgListVO();
                BeanUtil.copyProperties(data,orgListVO);
                return orgListVO;
            }).collect(Collectors.toList());
        }
        return result;
    }
}
